<?php defined('SYSPATH') or die('No direct script access.');

// Статьи
class Controller_Users extends Controller_Base
{
	/*public function action_index()
    {         
        $user = Auth::instance()
				->get_user();
		
        if (!$user)
            Request::current()->redirect('users/login');
		
		$title = '1232432 432432';
		
		$content = new View('user/info_tpl');
		$content->user = $user;
		
		$this->print_template($title, $content);
    }*/
	
    public function action_register()
    {		
		$message = null;
		$errors  = array();
             
        if (HTTP_Request::POST == $this->request->method())
        {          
            try
			{				
				$user = $this->user_model->add_user($this->request->post());
                 
                $_POST = array();
                          
                Request::current()->redirect('login'); 
            }
			catch (ORM_Validation_Exception $e)
			{
                $message = 'Форма заполнена с ошибками!';
                $errors = $e->errors('models');
            }
        }
		
		$title = 'Регистрация';
		
		$content = new View('register_tpl');
		$content->message = $message;
		$content->errors  = $errors;
		
		$this->print_template($title, $content);
    }
     
    public function action_login()
    {
        $message = null;
             
        if (HTTP_Request::POST == $this->request->method())
        {
            $remember = array_key_exists('remember', $this->request->post()) ? (bool)$this->request->post('remember') : false;
            $user = Auth::instance()->login($this->request->post('username'), $this->request->post('password'), $remember);
             
            if ($user)
                Request::current()->redirect('/');
            else
                $message = 'Вы ввели неправльный логин или пароль!';
        }
		
		$title = 'Вход';
		
		$content = new View('login_tpl');
		$content->message = $message;
		
		$this->print_template($title, $content);
    }
     
    public function action_logout()
    {
        Auth::instance()->logout();
        Request::current()->redirect('/');
    }

} // End Users
